import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @作者 ： pwx5328113 @时间 ： 2023/2/27 @测试点 : setObject方法参数覆盖
 **/
public class JDBC_Set_Get_Object_Case0025 {

	public static void main(String[] args) throws Exception {
		if (args.length < 4) {
			System.out.print("need para: host:ip,db_b,user,password");
			return;
		}
		String url = "jdbc:postgresql://" + args[0] + "/" + args[1] + "?LoggerLevel=TRACE";
		Connection connection = DriverManager.getConnection(url, args[2], args[3]);
		Statement statement = connection.createStatement();
		statement.executeUpdate("drop table if exists t_test; "
				+ "create table t_test (col_1 text,col_2 int,col_3 double precision,col_4 number)");
		PreparedStatement preparedStatement = connection
				.prepareStatement("insert into t_test (col_1,col_2,col_3,col_4) values (?, ?, ?, ?)");
		BigDecimal bigDecimal = new BigDecimal("1021.3275368745406131");
		preparedStatement.setObject(1, bigDecimal.toString());// 2个入参
		preparedStatement.setObject(2, bigDecimal, JDBCType.INTEGER);// 3个入参
		preparedStatement.setObject(3, bigDecimal, JDBCType.NUMERIC.getVendorTypeNumber(), 1);// 4个入参
		preparedStatement.setObject(4, bigDecimal, JDBCType.NUMERIC.getVendorTypeNumber(), 3);// 4个入参，第4入参不同值ֵ
		preparedStatement.executeUpdate();
		String col1, col2, col3, col4;
		try (ResultSet resultSet = statement.executeQuery("select * from t_test")) {
			if (resultSet.next()) {
				col1 = resultSet.getObject(1).toString();
				col2 = resultSet.getObject(2).toString();
				col3 = resultSet.getObject(3).toString();
				col4 = resultSet.getObject(4).toString();
				System.out.println("col1 = " + col1);
				System.out.println("col2 = " + col2);
				System.out.println("col3 = " + col3);
				System.out.println("col4 = " + col4);
				if (col1.equals("1021.3275368745406131") && col2.equals("1021") && col3.equals("1021.3")
						&& col4.equals("1021.328")) {
					System.out.println("The setObject function is executed successfully");
				}
			}
		}
		statement.executeUpdate("drop table t_test");
		preparedStatement.close();
		statement.close();
		connection.close();
	}
}
